﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace AccessCommon
{
    public static class ConvertClass
    {
        public static int ToInt32(object obj)
        {
            if (obj != null && obj.ToString() !=string.Empty )
            {
                try
                {
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public static int? ToInt(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static long ToLong(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    return Convert.ToInt64(obj);
                }
                catch
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public static decimal ToDecimal(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    decimal d1 = 0;
                    decimal.TryParse(obj.ToString(), System.Globalization.NumberStyles.Currency, null, out d1);
                  //  decimal.TryParse(obj.ToString(), out d1);
                    //double.TryParse(s, System.Globalization.NumberStyles.Currency,null, out d1)
                   // return Convert.ToDecimal(obj);
                    return d1;
                }
                catch
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public static decimal ToDecimal(string obj,CultureInfo culture)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    decimal d1 = 0;
                    decimal.TryParse(obj, System.Globalization.NumberStyles.Currency, culture, out d1);
                    //  decimal.TryParse(obj.ToString(), out d1);
                    //double.TryParse(s, System.Globalization.NumberStyles.Currency,null, out d1)
                    // return Convert.ToDecimal(obj);
                    return d1;
                }
                catch
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        public static DateTime? ToDataTime(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    return Convert.ToDateTime(obj);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static DateTime? ToDateTime(string p)
        {
            if (p != null && p!=string.Empty)
            {
                try
                {
                    return Convert.ToDateTime(p);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static byte? ToByte(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    return Convert.ToByte(obj);
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static float? ToFloat(object obj)
        {
            if (obj != null && obj.ToString() != string.Empty)
            {
                try
                {
                    return float.Parse(obj.ToString());
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

     

        /// <summary>
        /// 获取当月第几周
        /// </summary>
        /// <param name="day"></param>
        /// <param name="WeekStart"> 1表示周一至周日为一周 2表示 周日至周六为一周  </param>
        /// <returns></returns>
        public static int ToWeekOfMonth(DateTime day, int WeekStart)
        {
            //WeekStart                                                                      
            //1表示 周一至周日为一周                                                        
            //2表示 周日至周六为一周  
            try
            {
                DateTime FirstofMonth;
                FirstofMonth = Convert.ToDateTime(day.Date.Year + "-" + day.Date.Month + "-" + 1);

                int i = (int)FirstofMonth.Date.DayOfWeek;
                if (i == 0)
                {
                    i = 7;
                }

                if (WeekStart == 1)
                {
                    return (day.Date.Day + i - 2) / 7 + 1;
                }
                if (WeekStart == 2)
                {
                    return (day.Date.Day + i - 1) / 7 + 1;

                }
                return 0;
            }
            catch
            {
                return 0;
                //错误返回值0 
            }                                                                   
        }         



    }
}
